<template>
  <div id="app">
    <div class="card-caontainer">
      <div class="expand" @click="expandAllStateHandler">全部展开</div>
      <el-tree
        class="filter-tree"
        :data="data"
        :props="defaultProps"
        :filter-node-method="filterNode"
        ref="tree"
        highlight-current
        :default-expanded-keys="['xxx']"
        :default-checked-keys="[4]"
      />
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    expandAllStateHandler() {
      this.expandAllState = !this.expandAllState;
      console.log(this.expandAllState);
      this.$refs.tree.$children.forEach(ele => {
        if (ele.expanded !== this.expandAllState) {
          ele.expanded = !this.expandAllState;
          ele.handleClick();
        }
      });
    }
  },

  data() {
    return {
      data: [
        {
          id: "xxx",
          label: "一级 1",
          children: [
            {
              id: 4,
              label: "二级 1-1"
            }
          ]
        },
        {
          id: 2,
          label: "一级 2",
          children: [
            {
              id: 5,
              label: "二级 2-1"
            },
            {
              id: 6,
              label: "二级 2-2"
            }
          ]
        },
        {
          id: 3,
          label: "一级 3",
          children: [
            {
              id: 7,
              label: "二级 3-1"
            },
            {
              id: 8,
              label: "二级 3-2"
            }
          ]
        }
      ],
      expandAllState: false, // 展开状态
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  }
};
</script>
<style lang="less" scoped>
@import "~style/basic-css/variable.less";
@import "~style/subjectPortrait.less";
.card-caontainer {
  width: 180px;
  height: 297px;
  border-radius: 2px 0px 0px 2px;
  border: 1px solid #d6def8;
  /deep/.el-icon-caret-right {
    width: 16px;
    text-align: center;
    &.expanded {
      transform: rotate(-180deg);
    }
    &:before {
      content: "\e6df";
    }
  }
  .expand {
    font-size: 12px;
    cursor: pointer;
    user-select: none;
    height: 28px;
    line-height: 28px;
    margin-left: 28px;
  }
  /deep/ .el-tree-node__label {
    font-size: 12px;
    color: @fontColor;
  }
  /deep/ .el-tree-node__content {
    margin-top: 2px;
    margin-bottom: 2px;
    height: 20px;
  }
  /deep/.el-tree-node > .el-tree-node__content {
    background-color: transparent !important;
    &:hover {
      background-color: @mousePress !important;
    }
  }
  /deep/.el-tree-node__children > .is-current > .el-tree-node__content {
    background-color: @mousePress !important;
    font-weight: 600;
    .el-tree-node__label {
      color: @mainPress;
    }
  }
}
</style>
